package com.sdkj.recruit.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Persistent;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.IMongodbDAO;
import com.sdkj.recruit.dao.IUserResumeDao;
import com.sdkj.recruit.model.CompanyResume;
import com.sdkj.recruit.model.EducationalExperience;
import com.sdkj.recruit.model.Resume;
import com.sdkj.recruit.model.WorkExperience;
import com.sdkj.recruit.mongoSupport.QueryParamCreator;
import com.sdkj.recruit.util.StringUtil;

@Persistent
@Service("resumeDao")
public class UserResumeDao implements IUserResumeDao {

	@Autowired
	private IMongodbDAO dao;

	@Override
	public String saveResume(Resume resume) {

		dao.saveOrUpdatePO(resume);
		return resume.getResumeId();
	}

	@Override
	public void deleteResume(String resumeId) {
		dao.deletePOList(Resume.class, QueryParamCreator.eq("resumeId", resumeId));
	}

	@Override
	public Resume queryResume(String resumeId) {
		return dao.getPO(Resume.class, QueryParamCreator.eq("resumeId", resumeId));
	}

	@Override
	public String saveWorkExperience(WorkExperience vo) {
		dao.saveOrUpdatePO(vo);
		return vo.getId();
	}

	@Override
	public String saveEducationalExperience(EducationalExperience vo) {
		dao.saveOrUpdatePO(vo);
		return vo.getId();
	}

	@Override
	public void saveEducationalExperience(List<EducationalExperience> eduList) {
		dao.saveOrUpdatePOList(eduList);
	}

	@Override
	public void saveWorkExperience(List<WorkExperience> workList) {
		dao.saveOrUpdatePOList(workList);
	}

	@Override
	public List<EducationalExperience> queryEduList(String resumeId) {
		return dao.getPOList(EducationalExperience.class, QueryParamCreator.eq("resumeId", resumeId));
	}

	@Override
	public List<WorkExperience> queryWorkList(String resumeId) {
		return dao.getPOList(WorkExperience.class, QueryParamCreator.eq("resumeId", resumeId));
	}

	@Override
	public void deleteWorkList(String resumeId) {
		dao.deletePOList(WorkExperience.class, QueryParamCreator.eq("resumeId", resumeId));
	}

	@Override
	public void deleteEduList(String resumeId) {
		dao.deletePOList(EducationalExperience.class, QueryParamCreator.eq("resumeId", resumeId));
	}

	@Override
	public void updateResume(Resume resume) {
		Map<String,Object> map =new HashMap<String,Object>();
		if(!StringUtil.isEmptyString(resume.getUserName())) {
			map.put("userName", resume.getUserName());
		}
		
		if(resume.getSex()>0) {
			map.put("sex", resume.getSex());
		}
		
		if(resume.getBirthday() !=null) {
			map.put("birthday", resume.getBirthday());
		}
		
		if(!StringUtil.isEmptyString(resume.getSalaryBegin())) {
			map.put("salaryBegin", resume.getSalaryBegin());
		}
		
		if(!StringUtil.isEmptyString(resume.getSalaryEnd())) {
			map.put("salaryEnd", resume.getSalaryEnd());
		}
		
		if(!StringUtil.isEmptyString(resume.getIntroduce())) {
			map.put("introduce", resume.getIntroduce());
		}
		
		if(resume.getWorkYear()>0) {
			map.put("workYear", resume.getWorkYear());
		}
		dao.saveOrUpdateDocument(Resume.class, map, QueryParamCreator.eq("resumeId", resume.getResumeId()));
	}

	@Override
	public void updateEducationalExperience(List<EducationalExperience> eduList) {
		dao.saveOrUpdatePOList(eduList);
	}

	@Override
	public void updateWorkExperience(List<WorkExperience> workList) {
		dao.saveOrUpdatePOList(workList);
	}

	@Override
	public void updateWorkExperience(WorkExperience we) {
		dao.saveOrUpdatePO(we);
	}

	@Override
	public void updateEducationalExperience(EducationalExperience we) {
		dao.saveOrUpdatePO(we);
	}

	@Override
	public void deleteEducationalExperience(String id) {
		dao.deletePOList(EducationalExperience.class, QueryParamCreator.eq("_id", id));
	}

	@Override
	public void deleteWorkExperience(String id) {
		dao.deletePOList(WorkExperience.class, QueryParamCreator.eq("_id", id));
	}

	@Override
	public void sendResume(CompanyResume cr) {
		dao.saveOrUpdatePO(cr);
	}

	@Override
	public Resume queryResumeByUserId(String userId) {
		return dao.getPO(Resume.class, QueryParamCreator.eq("userId", userId));
	}

	@Override
	public CompanyResume queryCompanyResume(String userId, String resumeId, String companyId, String positionId) {
		return dao.getPO(CompanyResume.class, QueryParamCreator.eq("resumeId", resumeId),QueryParamCreator.eq("companyId", companyId),QueryParamCreator.eq("positionId", positionId));
	}

}
